/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Business;

import Connections.Database;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author tuanba_b00920
 */
public class SpaceUnitMeasure extends Database{
    Connection conn=null;
  public SpaceUnitMeasure()
 {
    
  }

public boolean Add(String symbol)
{
    conn=getConn();
    try{
        PreparedStatement prsm=conn.prepareStatement("Insert into SpaceUnitMeasure(symbol) values (?)");
          prsm.setString(1,symbol );
          prsm.execute();
          closeConn();
          return true;
     }catch(Exception e){
        JOptionPane.showMessageDialog(null,"Insert Error !");
        closeConn();
  }
  return false;
 }
   public Vector<String> getListToVector(){
        conn=getConn();
        Vector<String> vt = new Vector<String>();
        try {

            PreparedStatement prsm = conn.prepareStatement("Select * from SpaceUnitMeasure");
            ResultSet rs = prsm.executeQuery();
            while (rs.next()) {
                vt.add(rs.getString("symbol"));
            }
           closeConn();
        } catch (SQLException ex) {
            Logger.getLogger(WeightUnitMeasure.class.getName()).log(Level.SEVERE, null, ex);
            closeConn();
        }
        return vt;
    }
public boolean checkSymbol(String name)
 {   conn=getConn();
     boolean flag=false;
    try{
          PreparedStatement prsm=conn.prepareStatement("Select symbol from SpaceUnitMeasure where symbol=?");
          prsm.setString(1, name);
          ResultSet rs=prsm.executeQuery();
          while(rs.next()){
           flag=true;
          }
          closeConn();
     }
     catch(Exception e)
     {
         System.out.println(e);
         flag=false;
         closeConn();
     }
     return flag;
  }
  public void Edit(String symbol1,String symbol2)
    {
      conn=getConn();
       try{
           PreparedStatement prsm=conn.prepareStatement("Update SpaceUnitMeasure set symbol = ? where symbol=?");
            prsm.setString(1, symbol1);
            prsm.setString(2, symbol2);
            prsm.executeUpdate();
            closeConn();
       }catch(Exception e){
           System.out.println(e);
          closeConn();
       }
    }
    public void Delete(String name)
   {
        conn=getConn();
     try{
         PreparedStatement prsm=conn.prepareStatement("Delete from SpaceUnitMeasure where symbol=?");
         prsm.setString(1, name);
         prsm.executeUpdate();
         closeConn();
     }
     catch(Exception e)
     {
         System.out.println(e);
         closeConn();
     }
    }
   public int GetID(String name)
    {
       conn=getConn();
       int id=0;
       try {
             PreparedStatement prsm=conn.prepareStatement("Select id from SpaceUnitMeasure where symbol like ?");
             prsm.setString(1,name);
             ResultSet rs=prsm.executeQuery();
             while(rs.next())
             {
               id=rs.getInt(1);
             }
       closeConn();
       } catch (Exception e) {
          //System.out.println(e);
           JOptionPane.showMessageDialog(null, "Error!");
           closeConn();
       }
       return id;
   }
     public String GetName(int id)
    {
       conn=getConn();
       String name="";
       try {
             PreparedStatement prsm=conn.prepareStatement("Select symbol from SpaceUnitMeasure where id=?");
             prsm.setInt(1,id);
             ResultSet rs=prsm.executeQuery();
             while(rs.next())
             {
               name=rs.getString("symbol");
             }
       closeConn();
       } catch (Exception e) {
           //e.printStackTrace();
           JOptionPane.showMessageDialog(null, "Error!");
           closeConn();
       }
       return name;
   }
   
}
